$(function(){
		$("#spelling").autocomplete("../epowerwordfrequency/queryWords.action",{
			minChars: 0,
			matchContains : true,
			scroll: false,
		    extraParams:{spelling:function(){return encodeURIComponent($("#spelling").val());}},
		    dataType: 'json',  
		    parse:parseFn, 
		    formatItem : function(row,i,max){return decodeURIComponent(row.spelling) ;},
		    formatMatch : function (row){return decodeURIComponent(row.spelling) ;},
		    formatResult : function(row){return decodeURIComponent(row.spelling);}
		}).result(function(event,data,formatted){
			 $("#spelling").val(data.spelling);
		}).blur(function(){
			$(this).val($.trim($(this).val()));
			if($(this).val()==""){
				$("#spelling").val("");
			}
	    });
	});
$(function(){
		$("#mainSpelling").autocomplete("../jsonepowermainword/queryMainEpowerWord.action",{
			minChars: 0,
			matchContains : true,
			scroll: false,
		    extraParams:{spelling:function(){return encodeURIComponent($("#mainSpelling").val());}},
		    dataType: 'json',  
		    parse:parseMainFn, 
		    formatItem : function(row,i,max){   return decodeURIComponent(row.spelling) ;},
		    formatMatch : function (row){return decodeURIComponent(row.spelling) ;},
		    formatResult : function(row){return decodeURIComponent(row.spelling);}
		}).result(function(event,data,formatted){
			 if(data){
				$("#groupid").val(data.id);
			}else{
				$("#groupid").val("");
			}
			$("#mainSpelling").val(data.spelling);
		}).blur(function(){
			$(this).val($.trim($(this).val()));
			if($(this).val()==""){
				$("#spelling").val("");
			}
	    });
	});
 function parseMainFn(data){
	var rows = [];
	for(var i=0; i<data.result.mainwords.length; i++){     
		rows[rows.length] = {      
			data:data.result.mainwords[i],      
	        value:decodeURIComponent(data.spelling),      
            result:decodeURIComponent(data.spelling)     
  		 };      
 	}
 	 if(rows.length<1){
 		$("#groupid").val("");
 	}
	return rows;     

}
 
function parseFn(data){
	var rows = [];
	for(var i=0; i<data.result.words.length; i++){     
		rows[rows.length] = {      
			data:data.result.words[i],      
	        value:decodeURIComponent(data.spelling),      
            result:decodeURIComponent(data.spelling)     
  		 };      
 	}
 	 if(rows.length<1){
 		$("#groupid").val("");
 	}
	return rows;     

}